package org.csu.logistics.Service;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fasterxml.jackson.databind.util.JSONPObject;
import org.csu.logistics.common.CommonResponse;
import org.csu.logistics.entity.Order;
import org.csu.logistics.entity.Warehouse;
import org.csu.logistics.persistence.OrderMapper;
import org.csu.logistics.persistence.WarehouseMapper;
import org.csu.logistics.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import springfox.documentation.spring.web.json.Json;

@Service("SumUpService")
public class SumUpServiceImp implements SumUpService{
    @Autowired
    OrderMapper orderMapper;
    @Autowired
    WarehouseMapper warehouseMapper;
    @Override
    public CommonResponse<Object> Sum_up() {

        SumUp sumUp = new SumUp();
        OrderStatusSumUp orderStatusSumUp = new OrderStatusSumUp();
        TransportModeSumUp transportModeSumUp = new TransportModeSumUp();
        TransCenterSumUp transCenterSumUp = new TransCenterSumUp();
        EvaluateSumUp evaluateSumUp = new EvaluateSumUp();

        //orderStatus
        QueryWrapper<Order> pendingWrapper = new QueryWrapper<>();
        pendingWrapper.eq("orderStatus","待处理");
        orderStatusSumUp.set待处理(orderMapper.selectList(pendingWrapper).size());

        QueryWrapper<Order> inTransitWrapper = new QueryWrapper<>();
        inTransitWrapper.eq("orderStatus","运输中");
        orderStatusSumUp.set运输中(orderMapper.selectList(inTransitWrapper).size());

        QueryWrapper<Order> finishedWrapper = new QueryWrapper<>();
        finishedWrapper.eq("orderStatus","已完成");
        orderStatusSumUp.set已完成(orderMapper.selectList(finishedWrapper).size());

        orderStatusSumUp.set售后(orderMapper.selectList(null).size()-orderMapper.selectList(finishedWrapper).size()-orderMapper.selectList(inTransitWrapper).size()-orderMapper.selectList(pendingWrapper).size());

        Object orderStatusSumUpJson = JSONObject.toJSON(orderStatusSumUp);
        sumUp.setOrderStatusSumUp(orderStatusSumUpJson);

        //transportMode
        QueryWrapper<Order> standardWrapper = new QueryWrapper<>();
        standardWrapper.eq("transportMode","标准快递");
        transportModeSumUp.set标准快递(orderMapper.selectList(standardWrapper).size());

        QueryWrapper<Order> specialWrapper = new QueryWrapper<>();
        specialWrapper.eq("transportMode","特准快件");
        transportModeSumUp.set特准快件(orderMapper.selectList(specialWrapper).size());

        QueryWrapper<Order> bulkyWrapper = new QueryWrapper<>();
        bulkyWrapper.eq("orderStatus","大件快递");
        transportModeSumUp.set大件快递(orderMapper.selectList(bulkyWrapper).size());

        Object transportModeJson = JSONObject.toJSON(transportModeSumUp);
        sumUp.setTransportModeSumUp(transportModeJson);

        //evaluate
        QueryWrapper<Order> oneWrapper = new QueryWrapper<>();
        oneWrapper.eq("evaluate",1);
        evaluateSumUp.set一分(orderMapper.selectList(oneWrapper).size());

        QueryWrapper<Order> twoWrapper = new QueryWrapper<>();
        twoWrapper.eq("evaluate",2);
        evaluateSumUp.set两分(orderMapper.selectList(twoWrapper).size());

        QueryWrapper<Order> threeWrapper = new QueryWrapper<>();
        threeWrapper.eq("evaluate",3);
        evaluateSumUp.set三分(orderMapper.selectList(threeWrapper).size());

        QueryWrapper<Order> fourWrapper = new QueryWrapper<>();
        fourWrapper.eq("evaluate",4);
        evaluateSumUp.set四分(orderMapper.selectList(fourWrapper).size());

        QueryWrapper<Order> fiveWrapper = new QueryWrapper<>();
        fiveWrapper.eq("evaluate",5);
        evaluateSumUp.set五分(orderMapper.selectList(fiveWrapper).size());

        Object evaluateJson = JSONObject.toJSON(evaluateSumUp);
        sumUp.setEvaluateSumUp(evaluateJson);

        //TransCenter
        QueryWrapper<Warehouse> ChangshaWrapper = new QueryWrapper<>();
        ChangshaWrapper.eq("warehouseid",1);
        transCenterSumUp.set长沙(warehouseMapper.selectList(ChangshaWrapper).size());

        QueryWrapper<Warehouse> WuhanWrapper = new QueryWrapper<>();
        WuhanWrapper.eq("warehouseid",2);
        transCenterSumUp.set武汉(warehouseMapper.selectList(WuhanWrapper).size());

        QueryWrapper<Warehouse> NanchangWrapper = new QueryWrapper<>();
        NanchangWrapper.eq("warehouseid",3);
        transCenterSumUp.set南昌(warehouseMapper.selectList(NanchangWrapper).size());

        QueryWrapper<Warehouse> ZhengzhouWrapper = new QueryWrapper<>();
        ZhengzhouWrapper.eq("warehouseid",4);
        transCenterSumUp.set郑州(warehouseMapper.selectList(ZhengzhouWrapper).size());

        QueryWrapper<Warehouse> ChengduWrapper = new QueryWrapper<>();
        ChengduWrapper.eq("warehouseid",5);
        transCenterSumUp.set成都(warehouseMapper.selectList(ChengduWrapper).size());

        QueryWrapper<Warehouse> GuangzhouWrapper = new QueryWrapper<>();
        GuangzhouWrapper.eq("warehouseid",6);
        transCenterSumUp.set广州(warehouseMapper.selectList(GuangzhouWrapper).size());

        QueryWrapper<Warehouse> ShanghaiWrapper = new QueryWrapper<>();
        ShanghaiWrapper.eq("warehouseid",7);
        transCenterSumUp.set上海(warehouseMapper.selectList(ShanghaiWrapper).size());

        QueryWrapper<Warehouse> BeijingWrapper = new QueryWrapper<>();
        BeijingWrapper.eq("warehouseid",8);
        transCenterSumUp.set北京(warehouseMapper.selectList(BeijingWrapper).size());

        Object transCenterSumUpJson = JSONObject.toJSON(transCenterSumUp);
        sumUp.setTransCenterSumUp(transCenterSumUpJson);

        Object json = JSONObject.toJSON(sumUp);


        return CommonResponse.createForSuccess(json);
    }
}
